package org.java3.loadagent;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * LoadLogback
 *
 * @author sunxh
 * @date 15/9/16
 */
public class LoadLogback {

    private static Logger logger = LoggerFactory.getLogger(LoadLogback.class);

    public static void premain(String agentArgs) {
        System.out.println("loadAgent works");
        System.out.println("agentArgs=" + agentArgs);

        String hello = "/*\n" +
                "                   _ooOoo_\n" +
                "                  o8888888o\n" +
                "                  88\" . \"88\n" +
                "                  (| -_- |)\n" +
                "                  O\\  =  /O\n" +
                "               ____/`---'\\____\n" +
                "             .'  \\\\|     |//  `.\n" +
                "            /  \\\\|||  :  |||//  \\\n" +
                "           /  _||||| -:- |||||-  \\\n" +
                "           |   | \\\\\\  -  /// |   |\n" +
                "           | \\_|  ''\\---/''  |   |\n" +
                "           \\  .-\\__  `-`  ___/-. /\n" +
                "         ___`. .'  /--.--\\  `. . __\n" +
                "      .\"\" '<  `.___\\_<|>_/___.'  >'\"\".\n" +
                "     | | :  `- \\`.;`\\ _ /`;.`/ - ` : | |\n" +
                "     \\  \\ `-.   \\_ __\\ /__ _/   .-` /  /\n" +
                "======`-.____`-.___\\_____/___.-`____.-'======\n" +
                "                   `=---='\n" +
                "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n" +
                "         佛祖保佑       永无BUG\n" +
                "*/";

        System.out.println(hello);

        Boolean canUse = true;

        if (agentArgs == null) {
            canUse = false;
        } else if (!agentArgs.matches(".*\\.xml")) {
            canUse = false;
        } else {
            File file = new File(agentArgs);
            if (!file.exists()) {
                canUse = false;
            }
        }


        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
        JoranConfigurator configurator = new JoranConfigurator();
        configurator.setContext(lc);
        lc.reset();
        try {
            if (canUse) {
                System.out.println("agentArgs 合法");
                String path = agentArgs;

                configurator.doConfigure(path);
            } else {
                try {
                    System.out.println("未检测到合法的agentArgs[logback配置]参数");
                    System.out.println("使用默认配置: mylogback.xml from agent");

                    Resource r = new Resource();
                    URL url = r.getLogbackResource();
                    System.out.println("url=" + url);

                    configurator.doConfigure(url);
                } catch (JoranException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
//                configurator.doConfigure("/app/java3/loadagent/loadagent-logback/src/main/resources/mylogback.xml");
        } catch (JoranException e) {
            e.printStackTrace();
        }
        StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
        System.out.println("===================");
        logger.debug("Hello {}", "debug message");

    }
}
